package com.moshme.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moshme.common.exception.ServiceException;
import com.moshme.project.domain.File;
import com.moshme.project.domain.StageCheckFile;
import com.moshme.project.domain.StageProgramme;
import com.moshme.project.service.FileService;
import com.moshme.project.service.StageProgrammeService;
import com.moshme.project.mapper.StageProgrammeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 19803
* @description 针对表【stage_programme(方案阶段)】的数据库操作Service实现
* @createDate 2023-09-23 11:56:04
*/
@Service
public class StageProgrammeServiceImpl extends ServiceImpl<StageProgrammeMapper, StageProgramme>
    implements StageProgrammeService{

    @Autowired
    private FileService fileService;

    @Value("${moshme.profile}")
    private String storagePath;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String upload(MultipartFile file, Long projectId) {
        LambdaQueryWrapper<StageProgramme> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<StageProgramme> eq = lambdaQueryWrapper.eq(StageProgramme::getProjectId, projectId);
        StageProgramme stageProgramme = getOne(eq);
        String networkPath = null;
        if (stageProgramme != null) {
            LambdaQueryWrapper<File> fileLambdaQueryWrapper = new LambdaQueryWrapper<>();
            File oldfile = fileService.getOne(fileLambdaQueryWrapper.eq(File::getNetworkPath, stageProgramme.getFilePath()));
            fileService.remove(fileLambdaQueryWrapper.eq(File::getNetworkPath, stageProgramme.getFilePath()));
            java.io.File ioFile = new java.io.File(oldfile.getLocalPath());
            if (ioFile.exists()) {
                if (!ioFile.delete()) {
                    throw new ServiceException("文件更新失败");
                }
            }
            networkPath = fileService.upload(file, "项目方案阶段/" + projectId);
            stageProgramme.setFilePath(networkPath);
            update(stageProgramme, eq);
        } else {
            networkPath = fileService.upload(file, "项目方案阶段/" + projectId);
            stageProgramme = new StageProgramme();
            stageProgramme.setFilePath(networkPath);
            stageProgramme.setProjectId(projectId);
            save(stageProgramme);
        }
        return networkPath;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delFile(Long projectId) {
        LambdaQueryWrapper<StageProgramme> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<StageProgramme> eq = lambdaQueryWrapper.eq(StageProgramme::getProjectId, projectId);
        StageProgramme stageProgramme = getOne(eq);
        if (stageProgramme == null) {
            throw new ServiceException("文件不存在");
        }
        remove(eq);
        LambdaQueryWrapper<File> fileLambdaQueryWrapper = new LambdaQueryWrapper<>();
        fileService.remove(fileLambdaQueryWrapper.eq(File::getNetworkPath, stageProgramme.getFilePath()));
        String filePath = storagePath + "/项目方案阶段/" + projectId+ "/";
        Path path = Paths.get(filePath);
        try {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                // 先去遍历删除文件
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                }

                // 再去遍历删除目录
                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                }

            });
        } catch (IOException e) {
            throw new ServiceException("文件删除失败");
        }
        return true;
    }
}




